package com.example.taximeter.controller;

import com.example.taximeter.bean.*;
import com.example.taximeter.service.CompanyService;
import com.example.taximeter.service.PriceService;
import com.example.taximeter.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 价格类型
 */
@RestController
@RequestMapping(value = "/price")
public class PriceController {

    private PriceService priceService;
    private UserService userService;
//    private final static Logger logger = LoggerFactory.getLogger(ParamFileController.class);


    @Autowired
    public PriceController(PriceService priceService,UserService userService){
        this.priceService = priceService;
        this.userService =userService;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<Price> findPriceList(){
        return priceService.findPriceList();
    }

    //查看价格类型列表
    @RequestMapping(value = "/all",method = RequestMethod.POST)
    public ReturnInfo findPriceAll(@RequestParam(value = "pageNum", defaultValue = "0") String pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "0") String pageSize,
                                  @RequestBody Price price) {
        //添加日志
        userService.addUserLog(7,price.toString());
        return priceService.findPriceAll(Integer.valueOf(pageNum), Integer.valueOf(pageSize), price);
    }

    //新增价格类型
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ReturnInfo addPrice(@RequestBody  @Validated(GroupCreate.class) Price price){
        String mes = priceService.addPrice(price);
        if(mes.equals("成功")){
//            logger.info("新增价格类型"+price.toString());
            //添加日志
            userService.addUserLog(4,price.toString());
        }
        return ReturnInfo.info1(mes);
    }

    //修改价格类型
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public ReturnInfo updatePrice(@RequestBody  @Validated(GroupUpdate.class) Price price){
        String mes = priceService.updatePrice(price);
        if(mes.equals("成功")){
//            logger.info("修改价格类型"+price.toString());
            //添加日志
            userService.addUserLog(5,price.toString());
        }
        return ReturnInfo.info1(mes);
    }

    //删除价格类型
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
    public ReturnInfo deletePrice(@PathVariable Integer id){
        String mes = priceService.deletePrice(id);
        if(mes.equals("成功")){
//            logger.info("删除价格类型"+String.valueOf(id));
            //添加日志
            userService.addUserLog(6,String.valueOf(id));
        }
        return ReturnInfo.info1(mes);
    }
}
